package com.jmb.api.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmb.api.common.exception.WebApiException;
import com.jmb.api.common.result.ResultCodeEnum;
import com.jmb.api.entity.Project;
import com.jmb.api.vo.project.ProjectInfoVO;
import com.jmb.api.vo.project.ProjectModuleNameVO;
import com.jmb.api.vo.project.ProjectQueryVO;
import com.jmb.api.web.service.ProjectService;
import com.jmb.api.web.mapper.ProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 金梦博
* @description 针对表【t_project】的数据库操作Service实现
* @createDate 2025-09-25 17:20:38
*/
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project>
    implements ProjectService{
    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public void saveOrUpdateProject(Project project) {
        // 判斷是新增還是修改
        boolean isUpdate = project.getId() != null;


        // 检查项目名是否存在
        LambdaQueryWrapper<Project> projectWrapper = new LambdaQueryWrapper<>();
        projectWrapper.eq(Project::getName, project.getName());

        // 如果是修改，需要排除当前项目
        if (isUpdate){
            projectWrapper.ne(Project::getId, project.getId());
        }
        // 判断项目名是否存在
        if (projectMapper.selectCount(projectWrapper) > 0){
            throw new WebApiException(ResultCodeEnum.web_Project_EXIST_ERROR);
        }
        super.saveOrUpdate(project);
    }

    @Override
    public IPage<ProjectInfoVO> listProjectInfo(IPage<ProjectInfoVO> page, ProjectQueryVO projectQueryVO) {
        return projectMapper.listProjectInfo(page, projectQueryVO);
    }

    @Override
    public List<ProjectModuleNameVO> getProjectName() {
        return projectMapper.selectProjectName();
    }
}




